package me.zhengjie.inter.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.AdlXmxx;
import me.zhengjie.business.domain.AdlZhsq;
import me.zhengjie.business.domain.Snry;
import me.zhengjie.business.repository.AdlZhsqRepository;
import me.zhengjie.business.service.AdlXmxxService;
import me.zhengjie.business.service.AdlZhsqService;
import me.zhengjie.business.service.dto.AdlXmxxDto;
import me.zhengjie.business.service.dto.AdlZhsqDto;
import me.zhengjie.business.service.dto.SnryDto;
import me.zhengjie.business.service.mapstruct.AdlXmxxMapper;
import me.zhengjie.business.service.mapstruct.AdlZhsqMapper;
import me.zhengjie.domain.LocalStorage;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.inter.service.MpAdlZhsqService;
import me.zhengjie.repository.LocalStorageRepository;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @description 服务接口
 * @author xiahongbo
 * @date 2022-07-10
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class MpAdlZhsqServiceImpl implements MpAdlZhsqService {

    private final AdlZhsqRepository adlZhsqRepository;
    private final AdlZhsqMapper adlZhsqMapper;
    private final AdlXmxxService adlXmxxService;

    private final AdlZhsqService adlZhsqService;
//    private final Adl

    private final LocalStorageRepository localStorageRepository;


    @Override
    public AdlZhsqDto create(AdlZhsq resources, String ids) {


        AdlXmxxDto adlXmxxDto = adlXmxxService.findById(resources.getAdlXmxxId());
        resources.setAdlXmxxName(adlXmxxDto.getBisAdlXmxxName());
        resources.setAdlXmxxFs(adlXmxxDto.getBisAdlXmxxFs());
        resources.setAdlXmId(adlXmxxDto.getAdlXm().getBisAdlXmId());
        resources.setYwlx("RYLX02");
        resources.setBisUserId(SecurityUtils.getCurrentUserId());
        if(resources.getBisAdlZhsqId() == null || resources.getBisAdlZhsqId().equals("")){
            resources.setBisAdlZhsqId(IdUtil.simpleUUID());
        } else {
            AdlZhsqDto adlZhsqDto = adlZhsqService.findById(resources.getBisAdlZhsqId());
            resources.setLrr(adlZhsqDto.getLrr());
            resources.setLrrxm(adlZhsqDto.getLrrxm());
            resources.setLrsj(adlZhsqDto.getLrsj());
        }




        // 设置选项名称和分数
        AdlZhsqDto adlZhsq = adlZhsqMapper.toDto(adlZhsqRepository.save(resources));
        log.info("移动端[MpAdlZhsqServiceImpl - create]记录ADL量表{} ", adlZhsq);
        // 新增照片
        if(!StringUtils.isEmpty(ids)) {
            String[] picIds = ids.split(",");
            for (String picId:picIds) {

                Long storage_id = Long.parseLong(picId);
                LocalStorage localStorage = localStorageRepository.findById(storage_id).orElseGet(LocalStorage::new);
                localStorage.setYwid(String.valueOf(adlZhsq.getBisAdlZhsqId()));
                localStorageRepository.save(localStorage);
            }
        }
        return adlZhsq;
    }
}
